<?php
namespace Home\Controller;

use Common\Service\Curl;
use Think\Controller;

class IndexController extends Controller
{

    /**
     * key:   sit 50db2154f5d94017a134cdc61136638c pro 6b404ef5056d40fabe7e1f905f2887f4
     * secret:sit cc81ece2e3404d42b640731278859b77 pro a0128628fb36492cbeefe52b75156853
     * sit  eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI1MGRiMjE1NGY1ZDk0MDE3YTEzNGNkYzYxMTM2NjM4YyIsImV4cCI6MTUyODk3MjQ3MH0.w8PNvRY7UsnELuFbal0cUGFVYCy7KitFWb8GX2Ox5WQ
     * pro  eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI2YjQwNGVmNTA1NmQ0MGZhYmU3ZTFmOTA1ZjI4ODdmNCIsImV4cCI6MTUyOTM3Njg2MH0.DPT7fD_zRdXvGNrU9fYzQk2PiNDeqkPmHV7Rdjj43Mw
     */
    public function index()
    {            
        echo 22;
        exit();
        /*$payload = [ 'iss' => "6b404ef5056d40fabe7e1f905f2887f4", 'exp' => time() + 3600 ];
        echo $token = JWT::encode($payload, "a0128628fb36492cbeefe52b75156853");
        exit();*/

        //$apiUrl = "http://retesting.apple.myfuwu.com.cn&access_token={$token}";
        $token        = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI2YjQwNGVmNTA1NmQ0MGZhYmU3ZTFmOTA1ZjI4ODdmNCIsImV4cCI6MTUyOTM3Njg2MH0.DPT7fD_zRdXvGNrU9fYzQk2PiNDeqkPmHV7Rdjj43Mw';
        $redirect_uri = urlencode("http://hd.gzwkh.com/home/index/check");
        $userUrl      = "https://gzvanke.myfuwu.com.cn:886/cgi/auth/login?redirect_uri=" . $redirect_uri . "&appid=wx7639c41625861d06&access_token=" . $token;
        header("location:{$userUrl}");
        exit();
    }

    public function object_to_array($obj)
    {
        $obj = (array) $obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array) object_to_array($v);
            }
        }

        return $obj;
    }

    public function sendcode()
    {
        $mobile = I('mobile', '', '');
        //Log::write($mobile,'mobile');
        $url      = 'http://139.224.60.78:8888/sms.aspx';
        $pool     = '0123456789';
        $rand_key = '';
        for ($i = 0; $i < 4; $i++) {
            $rand_key .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
        }
        $params = array(
            'action'   => 'send',
            'account'  => 'gzwkh',
            'userid'   => '8805',
            'password' => 'gzwkh8984',
            'mobile'   => $mobile,
            'content'  => '【广州万客会】您的验证码：' . $rand_key,
            'action'   => 'send',
        );
        $res = Curl::request(POST, $url, $params);

        $xml     = simplexml_load_string($res);
        $xmljson = json_encode($xml);
        $arr     = json_decode($xmljson, true);

        //var_dump($arr);
        if ($arr['returnstatus'] == 'Success') {
            session('mobile', $mobile);
            session('rand_key', $rand_key);
            exit(json_encode(array('err' => 0, 'msg' => '验证码发送成功', 'res' => $rand_key)));
        } else {
            exit(json_encode(array('err' => 1, 'msg' => $arr['message'])));
        }
    }

    //curl post获取消息
    public function curl_post($url = "", $data = "")
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //设定为不验证证书和host
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $res = json_decode(curl_exec($ch));
        return $res;
    }
}
